package org.wesc.boot.service.async;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * @author Wesley
 * @date 2021/4/16
 */
@Service
@Slf4j
public class AsyncTestHandler {

    @Async("testPoolTaskExecutor")
    public void asyncWithoutFuture(int num) {
        log.info("thread name: {}, num = {}", Thread.currentThread().getName(), num);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Async("testPoolTaskExecutor")
    public Future<String> asyncFuture(int num) {
        log.info("thread name: {}, num = {}", Thread.currentThread().getName(), num);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(String.valueOf(num));
    }

}
